package com.example.demo.service.bis.impl;

import com.example.demo.bean.sys.SysDict;
import com.example.demo.bean.bis.UserState;
import com.example.demo.repository.SysDictMapper;
import com.example.demo.repository.UserStateMapper;
import com.example.demo.service.bis.UserStateService;
import com.example.demo.service.exception.ErrorCode;
import com.example.demo.service.exception.ServiceException;
import com.example.demo.utils.ConstantUtils;
import com.example.demo.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * @author zzz
 * @Title: UserStateServiceImpl
 * @ProjectName test
 * @Description: TODO()
 * @date 2018/7/20
 */
@Service
@Transactional
public class UserStateServiceImpl implements UserStateService {

    @Autowired private UserStateMapper userStateMapper;

    @Autowired private SysDictMapper sysDictMapper;


    @Override
    public Boolean insert(UserState userStateser) {
        userStateser.setState(ConstantUtils.INTEGER_TWENTY_SEVEN);
        userStateser.setTime(DateUtils.getTime(null));

        List<SysDict> list = sysDictMapper.findById(13);
        if(list.size()>0){
            for (int i=0 ;i<list.size();i++){
                if(list.get(i).getName().equals(userStateser.getTypeName())){
                    userStateser.setType(list.get(i).getId());
                }
            }
        }
        if(userStateMapper.insert(userStateser)>0){
            return true;
        }
        return false;
    }

    @Override
    public List<UserState> findByDeptId(Integer id) {
        if( StringUtils.isEmpty(id)){
            throw  new ServiceException(ErrorCode.NULL_ERROR);
        }
        return userStateMapper.selectByPrimaryDeptId(id);
    }

    @Override
    public Boolean update(Integer id,Integer state) {
        if(StringUtils.isEmpty(id) ){
            throw  new ServiceException(ErrorCode.NULL_ERROR);
        }
        UserState userState = new UserState();
        userState.setId(id);
        userState.setState(state);
        userState.setApprovalTime(DateUtils.getTime(null));
        if (userStateMapper.updateByPrimaryKey(userState)>0){
            return true;
        }
        return false;
    }
}
